#include "RedBird2.h"
#include "ParabolicFlight.h"
#include "Game.h"
#include "Animation.h"
#include "AudioSource.h"
#include "GameOptions.h"

RedBird2::RedBird2(void)
: TexturedEntity(), m_exploding(false)
{
}

RedBird2::RedBird2(boost::shared_ptr<Scene> scene)
: TexturedEntity(scene, "data/textures/ingamebirds1.tga"), m_exploding(false)
{
}

RedBird2::~RedBird2(void)
{
}

bool RedBird2::onInitialize()
{
    // Specific initialization
    Dimension screenSize = m_scene->getScreenSize();
    setPixelsSize(Dimension(48.0f, 44.0f));
    setPositionInTexture(Vector3(902.0f, 126.0f, 0.0f));
    setSizeInTexture(Dimension(48.0f, 44.0f));

    // Create and attach a behaviour
    float Vx = 15.0f + (rand() / ((float)RAND_MAX + 1)) * 15.0f;
    if (m_position.x > (screenSize.width / 2.0f))
    {
        Vx = -1.0f * Vx;
    }
    float yMax = (screenSize.height / 2.0f) + (rand() / ((float)RAND_MAX + 1)) * (screenSize.height / 2.0f);
    ParabolicFlight* behaviour = new ParabolicFlight(this, m_position.x, m_position.y, Vx, yMax, 5.0f);
    attachBehaviour(behaviour);

    // Create and attach an animation
    Animation* animation = new Animation(this);
    animation->setLoop(true);
    if (m_position.x > (screenSize.width / 2.0f))
    {
        animation->setVerticalFlip(true);
    }

    // First animation frame
    AnimationFrame frame1(Vector3(902.0f, 126.0f, 0.0f), Dimension(48.0f, 44.0f), 1.50f);
    animation->addFrame(frame1);

    // Second animation frame
    AnimationFrame frame2(Vector3(950.0f, 126.0f, 0.0f), Dimension(48.0f, 44.0f), 1.50f);
    animation->addFrame(frame2);

    attachAnimation(animation);

    // Common initialization from base class
    return commonInit();
}

void RedBird2::onPrepare(float dT)
{
    // If bird is not exploding, check if mouse has been pressed over it
    if (!m_exploding)
    {
        if (dynamic_cast<Game*> (m_scene.get())->isEntityBladed(m_position, m_pixelsSize))
        {
            m_exploding = true;

            // Change current animation and behaviour
            detachBehaviour();
            attachExplosionAnimation();

            // Play destruction SFX
            GameOptions* globalOpt = GameOptions::getOptions();
            AudioSource* sfx = new AudioSource("red_destroyed", EFFECT);
            sfx->setAudioFile("data/audio/red_destroyed.ogg");
            sfx->setLoop(false);
            sfx->initialize();
            sfx->setVolume(globalOpt->sfxLevel());
            sfx->play();
        }
    } // if (!m_exploding)

    // Common process from base class
    commonPrepare(dT);

    // If bird is exploding, check if the animation clip has finished
    if (m_exploding)
    {
        if (m_animation->hasAnimationFinished())
        {
            m_canBeRemoved = true;
            dynamic_cast<Game*> (m_scene.get())->increaseScore(10);
        }
    }
}

void RedBird2::onDestroy(void)
{
    if (!m_exploding)
    {
        dynamic_cast<Game*> (m_scene.get())->decreaseLifes();
    }
}

void RedBird2::attachExplosionAnimation(void)
{
    Animation *animation = new Animation(this);

    // First animation frame
    AnimationFrame frame1(Vector3(905.0f, 78.0f, 0.0f), Dimension(50.0f, 48.0f), 0.1f);
    animation->addFrame(frame1);

    // Second animation frame
    AnimationFrame frame2(Vector3(170.0f, 344.0f, 0.0f), Dimension(112.0f, 112.0f), 0.1f);
    animation->addFrame(frame2);

    // Third animation frame
    AnimationFrame frame3(Vector3(168.0f, 566.0f, 0.0f), Dimension(124.0f, 124.0f), 0.1f);
    animation->addFrame(frame3);

    // Fourth animation frame
    AnimationFrame frame4(Vector3(876.0f, 844.0f, 0.0f), Dimension(124.0f, 124.0f), 0.1f);
    animation->addFrame(frame4);

    // Fifth animation frame
    AnimationFrame frame5(Vector3(168.0f, 692.0f, 0.0f), Dimension(124.0f, 124.0f), 0.1f);
    animation->addFrame(frame5);

    attachAnimation(animation);

    // Define a uniform size and angle for all explosions
    updatePixelsSize(Dimension(48.0f, 44.0f));
    setRotationAngles(Vector3(0.0f, 0.0f, 0.0f));
}
